﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
using UnityEngine.SceneManagement;

public class GameManager : Photon.MonoBehaviour 
{
	public PhotonNetworkManager photonNetworkManager;
	public SpellManager spellManager;
	public PlayUIManager uiManager;
	public SpawnManager spawnManager;
	public Inventory inventory;
	[HideInInspector]public GameObject localPlayer;
	public MouseInteraction mouseInteraction;
	public CameraManager cameraManager;
	public InterfaceManager interfaceManager;
	public int localPlayerID;

	public static GameManager instance;
	public static GameManager Instance
	{
		get{return instance;}
		set{instance = value;}
	}

	void Awake()
	{
		if (instance != this)
			instance = this;
			photonNetworkManager.spawnManager = spawnManager;
	}
	public void SetLocalPlayer(GameObject me)
	{	
		spawnManager.SetLocalPlayer(me);
		localPlayer = me;
		mouseInteraction.player = me;
		photonNetworkManager.player = me;
		cameraManager.cameras[(int)CameraManager.cam.play] = me.GetComponent<PrefabObjectLinks>().cam;
		cameraManager.Play();
		localPlayerID = localPlayer.GetComponent<PhotonView>().viewID;
		localPlayer.GetComponent<PlayerController>().inventory = inventory;
		inventory.SetLocalPlayer(localPlayer.transform);
		localPlayer.GetComponent<LifeController>().inventory = inventory;
	}

	public void RemoveLocalPlayer()
	{	
		mouseInteraction.player = null;
		photonNetworkManager.player = null;
		spawnManager.RemoveLocalPlayer();
		cameraManager.Limbo();
	}

	public void LeaveGame()
	{
		RemoveLocalPlayer();
		photonNetworkManager.LeaveGame();
		SceneManager.LoadScene (GameConstants.PlayScene);
	}


}
